<?php
/**
 * User : Ciddy Natadisastra
 * Create Date: 7/22/11
 * Change Date: 7/22/11
 * File Description:
 *
 *
 *
 */
if ( !defined('SITE_PATH')) exit('No direct script access allowed');

class UserAuth extends model{
    private $activation;
    private $forgotCode;
    private $newUpass;

    private $method;
    private $userEmail;
    private $User;
    


    function __construct(){

    
    }
    private static function crypto($upass, $method=false){
        if(empty($upass)){
            return FALSE;
        } else {
            if($method = false){
                $cryptoUpass = strrev(base64_encode(strrev(base64_encode($upass))));
            } else {
                $cryptoUpass = base64_decode(strrev(base64_decode(strrev($upass))));
            }
            return $cryptoUpass;
        }
    }

    private static function userCode($userEmail, $User){
        function rot13encrypt ($userEmail, $User) {
            return str_rot13(base64_encode($userEmail.'|'.$User));
        }
    }

    private static function userDecode($userCode){
        function rot13decrypt ($userCode) {
            $code = base64_decode(str_rot13($userCode));
            return explode('|',$code);
        }
    }

    private static function translateUserDecode(){
        

    }

    private static function activate($userCode){
        //check db if exist

        //change status to active
    }

    private static function deactivate($userCode){
        //check db if exist

        //change status to active
    }

    private static function changePass($uid, $oldUpass, $newUpass){
        // check if id == pass

        //change pass

    }

    private static function forgotPassword($userCode){
        $code = self::userDecode($userCode);
        // check db

        // if true go to change pass

        // else goto forgot password page
        
    }

    public static function cekUname($uname){
        // select username if exist return true else false
    }

    public static function login($uname, $upass, $remember = false){
        if(!empty($uname) && !empty($upass) && self::cekUname($uname)){
            // query where uname where upass = 1

            $userData = array('uid'=>resultuid, 'uname'=>resultuname, 'uclass'=> resultclasss);
            $_SESSION['OKE_ID'] = $userData;

            // caranya echo $_SESSION['OKE_ID']['uname'];
            if($remember == true){
                $rannum = rand(123456789, 999999999);
                //update db with user_cookie rannum

                setcookie('OKE', base64_encode(resultuid.'|'.resultuname.'|'.resultemail.'|'.$rannum), time()+4800);

            }
        }
    }

    public static function checkLogin(){
        // check session first
        if(isset($_SESSION['OKE_ID'])){
            return TRUE;
        } elseif(isset($_COOKIE['OKE'])) {
            $code = explode('|', $_COOKIE['OKE']);

            //check db where uid = $code[0], uname = $code[1], umail = $code[2], user_cookie = $code[3]
            // if check db == 1
            // update where $code[0] user_cookie = $code[3]
            // insert session
            $userData = array('uid'=>$code[0], 'uname'=>$code[1], 'uclass'=> resultclasss);
            $_SESSION['OKE_ID'] = $userData;

            return TRUE;
        } else {
            return FALSE;
        }

        // check cookie 

    }

    public static function logout(){
        session_destroy();
        setcookie('OKE', '', time() - 4800);
    }

    public static function readSession($sessName){
        return $_SESSION['OKE_ID'][$sessName];
    }

    public static function readCookie($cookieName){
        $code = explode('|', $_COOKIE['OKE']);
        return $_COOKIE['oke'];
    }

}
